package com.haohan.jingyuan.customer.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.customer.dto.req.CreateIncomeRequest;
import com.haohan.jingyuan.customer.dto.req.CreateIncomeTypeRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteIncomeRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteIncomeTypeRequest;
import com.haohan.jingyuan.customer.dto.req.QueryIncomeListRequest;
import com.haohan.jingyuan.customer.dto.req.UpdateIncomeRequest;
import com.haohan.jingyuan.customer.dto.resp.IncomeInfoResp;
import com.haohan.jingyuan.customer.dto.resp.IncomeListResp;
import com.haohan.jingyuan.customer.dto.resp.IncomeTypeInfoResp;
import com.haohan.jingyuan.customer.dto.resp.IncomeTypeListResp;
import com.haohan.jingyuan.customer.service.CustomerIncomeService;
import com.haohan.jingyuan.customer.service.CustomerIncomeTypeService;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 客户收入控制器
 *
 * @ClassName CustomerIncomeController
 * @Description 客户收入和收入类型相关接口控制器
 * @Author hexijun
 * @Time 2025/01/19 10:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerIncomeController {

    /**
     * 客户收入服务
     */
    private final CustomerIncomeService customerIncomeService;

    /**
     * 客户收入类型服务
     */
    private final CustomerIncomeTypeService customerIncomeTypeService;

    /**
     * 新增收入
     *
     * @param request 新增收入请求
     * @return 收入信息
     */
    @PostMapping("/customer/income/create")
    @NeedLogin
    public GwResponse<IncomeInfoResp> createIncome(@Valid @RequestBody GwRequest<CreateIncomeRequest> request) {
        log.info("CustomerIncomeController.createIncome BEGIN ==> {}", JSONUtil.toJsonStr(request));

        IncomeInfoResp incomeInfo = customerIncomeService.createIncome(request.getRequestData());
        GwResponse<IncomeInfoResp> response = GwResponse.success("新增收入成功", incomeInfo);

        log.info("CustomerIncomeController.createIncome END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 更新收入
     *
     * @param request 更新收入请求
     * @return 收入信息
     */
    @PostMapping("/customer/income/update")
    @NeedLogin
    public GwResponse<IncomeInfoResp> updateIncome(@Valid @RequestBody GwRequest<UpdateIncomeRequest> request) {
        log.info("CustomerIncomeController.updateIncome BEGIN ==> {}", JSONUtil.toJsonStr(request));

        IncomeInfoResp incomeInfo = customerIncomeService.updateIncome(request.getRequestData());
        GwResponse<IncomeInfoResp> response = GwResponse.success("更新收入成功", incomeInfo);

        log.info("CustomerIncomeController.updateIncome END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询收入列表
     *
     * @param request 查询收入列表请求
     * @return 收入列表
     */
    @PostMapping("/customer/income/list")
    @NeedLogin
    public GwResponse<IncomeListResp> queryIncomeList(@Valid @RequestBody GwRequest<QueryIncomeListRequest> request) {
        log.info("CustomerIncomeController.queryIncomeList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        IncomeListResp incomeList = customerIncomeService.queryIncomeList(request.getRequestData());
        GwResponse<IncomeListResp> response = GwResponse.success("查询收入列表成功", incomeList);

        log.info("CustomerIncomeController.queryIncomeList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除收入
     *
     * @param request 删除收入请求
     * @return 删除结果
     */
    @PostMapping("/customer/income/delete")
    @NeedLogin
    public GwResponse<String> deleteIncome(@Valid @RequestBody GwRequest<DeleteIncomeRequest> request) {
        log.info("CustomerIncomeController.deleteIncome BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = customerIncomeService.deleteIncome(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除收入成功", null);
        } else {
            response = GwResponse.error("删除收入失败");
        }

        log.info("CustomerIncomeController.deleteIncome END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 创建收入类型
     *
     * @param request 创建收入类型请求
     * @return 收入类型信息
     */
    @PostMapping("/customer/incometype/create")
    @NeedLogin
    public GwResponse<IncomeTypeInfoResp> createIncomeType(@Valid @RequestBody GwRequest<CreateIncomeTypeRequest> request) {
        log.info("CustomerIncomeController.createIncomeType BEGIN ==> {}", JSONUtil.toJsonStr(request));

        IncomeTypeInfoResp incomeTypeInfo = customerIncomeTypeService.createIncomeType(request.getRequestData());
        GwResponse<IncomeTypeInfoResp> response = GwResponse.success("创建收入类型成功", incomeTypeInfo);

        log.info("CustomerIncomeController.createIncomeType END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询收入类型列表
     *
     * @return 收入类型列表
     */
    @PostMapping("/customer/incometype/list")
    @NeedLogin
    public GwResponse<IncomeTypeListResp> queryIncomeTypeList() {
        log.info("CustomerIncomeController.queryIncomeTypeList BEGIN");

        IncomeTypeListResp incomeTypeList = customerIncomeTypeService.queryIncomeTypeList();
        GwResponse<IncomeTypeListResp> response = GwResponse.success("查询收入类型列表成功", incomeTypeList);

        log.info("CustomerIncomeController.queryIncomeTypeList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除收入类型
     *
     * @param request 删除收入类型请求
     * @return 删除结果
     */
    @PostMapping("/customer/incometype/delete")
    @NeedLogin
    public GwResponse<String> deleteIncomeType(@Valid @RequestBody GwRequest<DeleteIncomeTypeRequest> request) {
        log.info("CustomerIncomeController.deleteIncomeType BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = customerIncomeTypeService.deleteIncomeType(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除收入类型成功", null);
        } else {
            response = GwResponse.error("删除收入类型失败");
        }

        log.info("CustomerIncomeController.deleteIncomeType END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}
